package lfm.mpr;

import java.util.ArrayList;


import db.AdminBaseDatosHelper;

import objects.Equipo;
import objects.Jugador;
import Toast.ToastLFM_Message;
import adapter.AdapterListEquipoDetalle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class JugadoresEquipoActivity_3_4_3  extends Activity{
	
	private ArrayList<Jugador> list_jugador_equipo;
	private AdminBaseDatosHelper miBBDDHelper;
	private static Context context;
	
	public static ImageButton Image_portero;
	public static ImageButton Image_defensa1;
	public static ImageButton Image_defensa2;
	public static ImageButton Image_defensa3;
	public static ImageButton Image_medio1;
	public static ImageButton Image_medio2;
	public static ImageButton Image_medio3;
	public static ImageButton Image_medio4;
	public static ImageButton Image_delantero1;
	public static ImageButton Image_delantero2;
	public static ImageButton Image_delantero3;
	
	private static TextView textView_port;
	public static TextView textView_def1;
	public static TextView textView_def2;
	public static TextView textView_def3;
	public static TextView textView_med1;
	public static TextView textView_med2;
	public static TextView textView_med3;
	public static TextView textView_med4;
	public static TextView textView_del1;
	public static TextView textView_del2;
	public static TextView textView_del3;
	
	public static TextView textView_precio_port;
	public static TextView textView_precio_def1;
	public static TextView textView_precio_def2;
	public static TextView textView_precio_def3;
	public static TextView textView_precio_med1;
	public static TextView textView_precio_med2;
	public static TextView textView_precio_med3;
	public static TextView textView_precio_med4;
	public static TextView textView_precio_del1;
	public static TextView textView_precio_del2;
	public static TextView textView_precio_del3;
	
	
	public static TextView TextView_presupuesto;
	private static TextView TextView_alineacion;
	
	public void onCreate(Bundle savedInstanceState) {
		 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view_jugadores_equipo_3_4_3);
        Bundle extras = getIntent().getExtras();
        context = JugadoresEquipoActivity_3_4_3.this;
        
        miBBDDHelper = new AdminBaseDatosHelper(JugadoresEquipoActivity_3_4_3.this);
        
        Image_portero = (ImageButton)findViewById(R.id.imageButton_portero);
        Image_defensa1 = (ImageButton)findViewById(R.id.imageButton_def1);
        Image_defensa2 = (ImageButton)findViewById(R.id.imageButton_def2);
        Image_defensa3 = (ImageButton)findViewById(R.id.imageButton_def3);
        Image_medio1 = (ImageButton)findViewById(R.id.imageButton_med1);
        Image_medio2 = (ImageButton)findViewById(R.id.imageButton_med2);
        Image_medio3 = (ImageButton)findViewById(R.id.imageButton_med3);
        Image_medio4 = (ImageButton)findViewById(R.id.imageButton_med4);
        Image_delantero1 = (ImageButton)findViewById(R.id.imageButton_del1);
        Image_delantero2 = (ImageButton)findViewById(R.id.imageButton_del2);
        Image_delantero3 = (ImageButton)findViewById(R.id.imageButton_del3);
        
        textView_port = (TextView)findViewById(R.id.textView_port);
    	textView_def1 = (TextView)findViewById(R.id.textView_def1);
    	textView_def2 = (TextView)findViewById(R.id.textView_def2);
    	textView_def3 = (TextView)findViewById(R.id.textView_def3);
    	textView_med1 = (TextView)findViewById(R.id.textView_med1);
    	textView_med2 = (TextView)findViewById(R.id.textView_medio2);
    	textView_med3 = (TextView)findViewById(R.id.textView1);
    	textView_med4 = (TextView)findViewById(R.id.textView2);
    	textView_del1 = (TextView)findViewById(R.id.textView_del1);
    	textView_del2 = (TextView)findViewById(R.id.textView_del2);
    	textView_del3 = (TextView)findViewById(R.id.textView_del3);
    	
    	textView_precio_port = (TextView)findViewById(R.id.textView_precio_portero);
    	textView_precio_def1 = (TextView)findViewById(R.id.textView_precio_def1);
    	textView_precio_def2 = (TextView)findViewById(R.id.textView_precio_def2);
    	textView_precio_def3 = (TextView)findViewById(R.id.textView_precio_def3);
    	textView_precio_med1 = (TextView)findViewById(R.id.textView_precio_med1);
    	textView_precio_med2 = (TextView)findViewById(R.id.textView_precio_med2);
    	textView_precio_med3 = (TextView)findViewById(R.id.textView_precio_med3);
    	textView_precio_med4 = (TextView)findViewById(R.id.textView_precio_med4);
    	textView_precio_del1 = (TextView)findViewById(R.id.textView_precio_del1);
    	textView_precio_del2 = (TextView)findViewById(R.id.textView_precio_del2);
    	textView_precio_del3 = (TextView)findViewById(R.id.textView_precio_del3);
    	
        
        
    	TextView_presupuesto = (TextView)findViewById(R.id.textView_presupuesto);
    	TextView_alineacion = (TextView)findViewById(R.id.textView_ali);
    	
    	if(TabHost_Team.team != null){
    			Equipo.Print_Equipo(TabHost_Team.team);
    			Set_Equipo(TabHost_Team.team);
    	}
        
    	Image_portero.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {	
            	Intent intent = new Intent(JugadoresEquipoActivity_3_4_3.this, SelectJugadorActivity.class);
            	intent.putExtra("posicion", "portero");
            	intent.putExtra("posicion_interna", "portero");
            	TabHost_Team.team.setModificado(1);
            	startActivity(intent);
            	
            }//end_onclick
        });//end_onclick_button_user
    	
    	
    	Image_defensa1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {	
            	Intent intent = new Intent(JugadoresEquipoActivity_3_4_3.this, SelectJugadorActivity.class);
            	intent.putExtra("posicion", "defensa");
            	intent.putExtra("posicion_interna", "def1");
            	TabHost_Team.team.setModificado(1);
            	/*if(TabHost_Team.team.getPortero()!= null) intent.putExtra("operation", "add");
        			else intent.putExtra("operation", "update");*/
            	startActivity(intent);
            }//end_onclick
        });//end_onclick_button_user
    	
    	Image_defensa2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {	
            	Intent intent = new Intent(JugadoresEquipoActivity_3_4_3.this, SelectJugadorActivity.class);
            	intent.putExtra("posicion", "defensa");
            	intent.putExtra("posicion_interna", "def2");
            	TabHost_Team.team.setModificado(1);
            	startActivity(intent);
            }//end_onclick
        });//end_onclick_button_user
    	
    	Image_defensa3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {	
            	Intent intent = new Intent(JugadoresEquipoActivity_3_4_3.this, SelectJugadorActivity.class);
            	intent.putExtra("posicion", "defensa");
            	intent.putExtra("posicion_interna", "def3");
            	TabHost_Team.team.setModificado(1);
            	startActivity(intent);
            }//end_onclick
        });//end_onclick_button_user
    	
    	Image_medio1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {	
            	Intent intent = new Intent(JugadoresEquipoActivity_3_4_3.this, SelectJugadorActivity.class);
            	intent.putExtra("posicion", "medio");
            	intent.putExtra("posicion_interna", "med1");
            	TabHost_Team.team.setModificado(1);
            	startActivity(intent);
            }//end_onclick
        });//end_onclick_button_user
    	
    	
    	Image_medio2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {	
            	Intent intent = new Intent(JugadoresEquipoActivity_3_4_3.this, SelectJugadorActivity.class);
            	intent.putExtra("posicion", "medio");
            	intent.putExtra("posicion_interna", "med2");
            	TabHost_Team.team.setModificado(1);
            	startActivity(intent);
            }//end_onclick
        });//end_onclick_button_user
    	
    	
    	Image_medio3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {	
            	Intent intent = new Intent(JugadoresEquipoActivity_3_4_3.this, SelectJugadorActivity.class);
            	intent.putExtra("posicion", "medio");
            	intent.putExtra("posicion_interna", "med3");
            	TabHost_Team.team.setModificado(1);
            	startActivity(intent);
            }//end_onclick
        });//end_onclick_button_user
    	
    	
    	Image_medio4.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {	
            	Intent intent = new Intent(JugadoresEquipoActivity_3_4_3.this, SelectJugadorActivity.class);
            	intent.putExtra("posicion", "medio");
            	intent.putExtra("posicion_interna", "med4");
            	TabHost_Team.team.setModificado(1);
            	startActivity(intent);
            }//end_onclick
        });//end_onclick_button_user
    	
    	
    	Image_delantero1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {	
            	Intent intent = new Intent(JugadoresEquipoActivity_3_4_3.this, SelectJugadorActivity.class);
            	intent.putExtra("posicion", "delantero");
            	intent.putExtra("posicion_interna", "del1");
            	TabHost_Team.team.setModificado(1);
            	startActivity(intent);
            }//end_onclick
        });//end_onclick_button_user
    	
    	Image_delantero2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {	
            	Intent intent = new Intent(JugadoresEquipoActivity_3_4_3.this, SelectJugadorActivity.class);
            	intent.putExtra("posicion", "delantero");
            	intent.putExtra("posicion_interna", "del2");
            	TabHost_Team.team.setModificado(1);
            	startActivity(intent);
            }//end_onclick
        });//end_onclick_button_user
    	
    	Image_delantero3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {	
            	Intent intent = new Intent(JugadoresEquipoActivity_3_4_3.this, SelectJugadorActivity.class);
            	intent.putExtra("posicion", "delantero");
            	intent.putExtra("posicion_interna", "del3");
            	TabHost_Team.team.setModificado(1);
            	startActivity(intent);
            }//end_onclick
        });//end_onclick_button_user
        
        
	}//end_onCreate
	
	
	private static void Set_Equipo(Equipo team){
		
		TextView_presupuesto.setText(new Integer(Equipo.Calcula_Precio_Acumulado_Equipo(team)).toString());
    	TextView_alineacion.setText(team.getAlineacion());
    	
    	if(team.getPortero()!= null){
    			textView_port.setText(team.getPortero().getNombre());
		        Image_portero.setImageResource(Get_imageResource(team.getPortero().getCodigo(), team.getPortero().getPosicion()));
		        textView_precio_port.setText(new Integer(team.getPortero().getPrecio()).toString());
    	}
    	if(team.getDefensas()!= null){	
    			for (int i = 0; i < team.getDefensas().size(); i++) {
					if(team.getDefensas().get(i).getPosicion_interna().equals("def1")){
						textView_def1.setText(team.getDefensas().get(i).getNombre());
						Image_defensa1.setImageResource(Get_imageResource(team.getDefensas().get(i).getCodigo(),
								team.getDefensas().get(i).getPosicion()));
						textView_precio_def1.setText(new Integer(team.getDefensas().get(i).getPrecio()).toString());
					}
					if(team.getDefensas().get(i).getPosicion_interna().equals("def2")){
						textView_def2.setText(team.getDefensas().get(i).getNombre());
						Image_defensa2.setImageResource(Get_imageResource(team.getDefensas().get(i).getCodigo(),
								team.getDefensas().get(i).getPosicion()));
						textView_precio_def2.setText(new Integer(team.getDefensas().get(i).getPrecio()).toString());
					}
					if(team.getDefensas().get(i).getPosicion_interna().equals("def3")){
						textView_def3.setText(team.getDefensas().get(i).getNombre());
						Image_defensa3.setImageResource(Get_imageResource(team.getDefensas().get(i).getCodigo(),
								team.getDefensas().get(i).getPosicion()));
						textView_precio_def3.setText(new Integer(team.getDefensas().get(i).getPrecio()).toString());
					}
				}
    		
    	}
    	if(team.getMedios()!= null){	
			for (int i = 0; i < team.getMedios().size(); i++) {
				if(team.getMedios().get(i).getPosicion_interna().equals("med1")){
					textView_med1.setText(team.getMedios().get(i).getNombre());
					Image_medio1.setImageResource(Get_imageResource(team.getMedios().get(i).getCodigo(),
							team.getMedios().get(i).getPosicion()));
					textView_precio_med1.setText(new Integer(team.getMedios().get(i).getPrecio()).toString());
				}
				if(team.getMedios().get(i).getPosicion_interna().equals("med2")){
					textView_med2.setText(team.getMedios().get(i).getNombre());
					Image_medio2.setImageResource(Get_imageResource(team.getMedios().get(i).getCodigo(),
							team.getMedios().get(i).getPosicion()));
					textView_precio_med2.setText(new Integer(team.getMedios().get(i).getPrecio()).toString());
				}
				if(team.getMedios().get(i).getPosicion_interna().equals("med3")){
					textView_med3.setText(team.getMedios().get(i).getNombre());
					Image_medio3.setImageResource(Get_imageResource(team.getMedios().get(i).getCodigo(),
							team.getMedios().get(i).getPosicion()));
					textView_precio_med3.setText(new Integer(team.getMedios().get(i).getPrecio()).toString());
				}
				if(team.getMedios().get(i).getPosicion_interna().equals("med4")){
					textView_med4.setText(team.getMedios().get(i).getNombre());
					Image_medio4.setImageResource(Get_imageResource(team.getMedios().get(i).getCodigo(),
							team.getMedios().get(i).getPosicion()));
					textView_precio_med4.setText(new Integer(team.getMedios().get(i).getPrecio()).toString());
				}
			}
		
	}
    	if(team.getDelanteros()!= null){	
			for (int i = 0; i < team.getDelanteros().size(); i++) {
				if(team.getDelanteros().get(i).getPosicion_interna().equals("del1")){
					textView_del1.setText(team.getDelanteros().get(i).getNombre());
					Image_delantero1.setImageResource(Get_imageResource(team.getDelanteros().get(i).getCodigo(),
							team.getDelanteros().get(i).getPosicion()));
					textView_precio_del1.setText(new Integer(team.getDelanteros().get(i).getPrecio()).toString());
				}
				if(team.getDelanteros().get(i).getPosicion_interna().equals("del2")){
					textView_del2.setText(team.getDelanteros().get(i).getNombre());
					Image_delantero2.setImageResource(Get_imageResource(team.getDelanteros().get(i).getCodigo(),
							team.getDelanteros().get(i).getPosicion()));
					textView_precio_del2.setText(new Integer(team.getDelanteros().get(i).getPrecio()).toString());
				}
				if(team.getDelanteros().get(i).getPosicion_interna().equals("del3")){
					textView_del3.setText(team.getDelanteros().get(i).getNombre());
					Image_delantero3.setImageResource(Get_imageResource(team.getDelanteros().get(i).getCodigo(),
							team.getDelanteros().get(i).getPosicion()));
					textView_precio_del3.setText(new Integer(team.getDelanteros().get(i).getPrecio()).toString());
				}
			}
		
	}
	}//Set_Equipo
	
	
	private static int Get_imageResource(int codigo, String posicion){
	
		String uri = "";
		if(posicion.equals("portero")){
			uri = "drawable/" + "p_" + String.valueOf(codigo);
		}
		else if(posicion.equals("defensa")){
			uri = "drawable/" + "def_" + String.valueOf(codigo);
		}
		else if(posicion.equals("medio")){
			uri = "drawable/" + "m_" + String.valueOf(codigo);
		}
		else{
			uri = "drawable/" + "del_" + String.valueOf(codigo);
		}
		
		int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
		Drawable img1;
        if(imageResource == 0) return R.drawable.user_profile;
        	else return imageResource;
	}
	
	
	@Override
	 public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
	    	 
	    	 boolean result = ToastLFM_Message.crear_Dialogo_Confirmacion(JugadoresEquipoActivity_3_4_3.this, "Si sale sin guardar, los cambios del equipo se perderan. Desea guardar los cambios ahora?");
	    	 if(result){
	    		 Save_Team();
	    		 Intent intent_home = new Intent(JugadoresEquipoActivity_3_4_3.this, MyTeamsLFMActivity.class);
	    		 startActivity(intent_home);
	    	 }
	    	 else {
	    		 Intent intent_home = new Intent(JugadoresEquipoActivity_3_4_3.this, MyTeamsLFMActivity.class);
	    		 startActivity(intent_home);}
	    }
	    return super.onKeyDown(keyCode, event);
	 }
	
	
	private void Save_Team(){
		if(TabHost_Team.Mode.equals("Edit")){
   		  
  		   miBBDDHelper.Update_Team_BD(TabHost_Team.team);
  		   miBBDDHelper.Remove_jugadores_equipo_BD(TabHost_Team.team);
  		   for (int i = 0; i < TabHost_Team.team.get_lista_jugadores().size(); i++) {
	   				miBBDDHelper.Add_jugador_equipo_BD(TabHost_Team.team.get_lista_jugadores().get(i), TabHost_Team.team);
				}
	   		   ToastLFM_Message.Show_Info_Toast(JugadoresEquipoActivity_3_4_3.this, "El Equipo se ha guardado correctamente");
	   		   Intent intent = new Intent(JugadoresEquipoActivity_3_4_3.this, MyTeamsLFMActivity.class);
	           startActivity(intent);
  	   }
  	   else {
      	   		int result = miBBDDHelper.New_Team_BD(TabHost_Team.team);
      	   		for (int i = 0; i < TabHost_Team.team.get_lista_jugadores().size(); i++) {
      	   			miBBDDHelper.Add_jugador_equipo_BD(TabHost_Team.team.get_lista_jugadores().get(i), TabHost_Team.team);
					}
      	   		if(result == 0) {
      	   			ToastLFM_Message.Show_Info_Toast(JugadoresEquipoActivity_3_4_3.this, "El Equipo se ha guardado correctamente");
      	   			Intent intent = new Intent(JugadoresEquipoActivity_3_4_3.this, MyTeamsLFMActivity.class);
      	           	startActivity(intent);
      	   		}
      	   		else Toast.makeText(JugadoresEquipoActivity_3_4_3.this, "Error, no ha sido posible guardar su equipo", Toast.LENGTH_SHORT).show();
  	   }
		
	}

}//JugadoresEquipoActivity
